﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCust_ContactPersonDAL:Iapp_tblCust_ContactPerson
    {
        HKEA_CMSEntities model;
        app_tblCust_ContactPerson cust_cp;
        private app_tblCust_ContactPersonDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCust_ContactPersonDAL GetInstance()
        {
            return new app_tblCust_ContactPersonDAL();
        }

        public int InsertENT(app_tblCust_ContactPerson ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCust_ContactPerson.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblCust_ContactPerson.Max(a => a.AppCustContactUID);
            return 0;
        }

        public List<M_ContactPersonListDetail> GetListByENT(int custUID)
        {
            List<M_ContactPersonListDetail> list = new List<M_ContactPersonListDetail>();

            var ret = from app_cust_cp in model.app_tblCust_ContactPerson
                      where app_cust_cp.CustomerUID == custUID && app_cust_cp.Deleted == 0
                      select app_cust_cp into appcpList
                      join app_cp in model.app_tblContactPerson
                      on appcpList.AppContactPersonUID equals app_cp.AppContactPersonUID

                      join app_info in model.app_tblContactInfo
                      on app_cp.AppContactInfoUID equals app_info.AppContactInfoUID into infoENT
                      from app_info in infoENT.DefaultIfEmpty()

                      join app_address in model.app_tblAddress
                      on app_cp.AppAddressUID equals app_address.AppAddressUID into addressENT
                      from app_address in addressENT.DefaultIfEmpty()

                      join salutation in model.mtblSalutations
                      on app_cp.SalutationUID equals salutation.SalutationUID into tempSalutation
                      from salutation in tempSalutation.DefaultIfEmpty()
                      select new M_ContactPersonListDetail
                      {
                          IsFromApp = true,
                          AppCustContactUID = appcpList.AppCustContactUID,
                          AppAddressUID =app_cp.AppAddressUID ?? 0,
                          AppContactPersonUID = app_cp.AppContactPersonUID,
                          AppInfoUID = app_cp.AppContactInfoUID ?? 0,
                           FirstName_Eng=app_cp.FirstName_Eng,
                           LastName_Eng=app_cp.LastName_Eng,
                          ChristianName = app_cp.ChristianName,
                          FirstName_Chi = app_cp.FirstName_Chi,
                          LastName_Chi = app_cp.LastName_Chi,
                          OtherTitle_Chi = app_cp.OtherTitle_Chi,
                          OtherTitle_Eng = app_cp.OtherTitle_Eng,
                          ContactPersonUID = appcpList.ContactPersonUID ?? 0,
                          Position_Chi = appcpList.Position_Chi,
                          Position_Eng = appcpList.Position_Eng,
                          Salutation = app_cp.mtblSalutation.Salutation_Eng
                      };
            list = ret.ToList();
            return list;
        }

        public List<app_tblCust_ContactPerson> GetListByENT(app_tblCust_ContactPerson ent, bool AddDeletedForSelect)
        {
            var result = model.app_tblCust_ContactPerson.Where(a=>a.AppCustContactUID > 0);
            if (ent.AppContactPersonUID > 0)
                result = result.Where(a=>a.AppContactPersonUID==ent.AppContactPersonUID);
            if (ent.AppCustContactUID > 0)
                result = result.Where(a=>a.AppCustContactUID==ent.AppCustContactUID);
            if (ent.CustContactUID > 0)
                result = result.Where(a=>a.CustContactUID==ent.CustContactUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a=>a.CustomerUID==ent.CustomerUID);
            
            if (AddDeletedForSelect)
                result = result.Where(a=>a.Deleted==ent.Deleted);
            return result.ToList();

        }

        public int GetCountByENT(app_tblCust_ContactPerson ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblCust_ContactPerson ent)
        {
            var temp = model.app_tblCust_ContactPerson.Where(a=>a.AppCustContactUID ==ent.AppCustContactUID).FirstOrDefault();
            if (temp != null)
            {
                temp.AppContactPersonUID = ent.AppContactPersonUID;
                temp.ContactPersonUID = ent.ContactPersonUID;
                temp.CustContactUID = ent.CustContactUID;
                temp.CustomerUID = ent.CustomerUID;
                temp.Deleted = ent.Deleted;
                temp.LastModifyBy = ent.LastModifyBy;
                temp.LastModifyDate = ent.LastModifyDate;
                temp.Position_Chi = ent.Position_Chi;
                temp.Position_Eng = ent.Position_Eng;
                temp.SourceUID = ent.SourceUID;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_tblCust_ContactPerson ent)
        {
            var result = model.app_tblCust_ContactPerson.Where(a=>a.AppCustContactUID > 0);
            if (ent.AppContactPersonUID > 0)
                result = result.Where(a=>a.AppContactPersonUID==ent.AppContactPersonUID);
            if (ent.AppCustContactUID > 0)
                result = result.Where(a=>a.AppCustContactUID==ent.AppCustContactUID);
            if (ent.ContactPersonUID > 0)
                result = result.Where(a=>a.ContactPersonUID ==ent.ContactPersonUID);
            if (ent.CustContactUID > 0)
                result = result.Where(a=>a.CustContactUID==ent.CustContactUID);
            if (ent.CustomerUID > 0)
                result = result.Where(a=>a.CustomerUID==ent.CustomerUID);
            foreach (var temp in result)
            {
                model.app_tblCust_ContactPerson.DeleteObject(temp);
            }
            return model.SaveChanges();
        }


        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblCust_ContactPerson.Where(a=>a.AppCustContactUID==temp).FirstOrDefault();
                if(result !=null)
                    model.app_tblCust_ContactPerson.DeleteObject(result);
            }
            return model.SaveChanges();
        }

        public List<int> GetCustomerUIDList()
        {
            return model.app_tblCust_ContactPerson.Where(a => a.CustomerUID > 0).Select(a => a.CustomerUID).ToList();
        }
    }
} 
